package com.jrx.anytxn.accounting.job.glinterfaceInvestor.config;

import com.jrx.anytxn.accounting.bean.GlInterfaceInvestorBean;
import com.jrx.anytxn.accounting.config.AnytxnBatchProperties;
import com.jrx.anytxn.accounting.job.glinterfaceInvestor.step.GlInterfaceInvestorListener;
import com.jrx.anytxn.accounting.job.glinterfaceInvestor.step.GlInterfaceInvestorProcessor;
import com.jrx.anytxn.accounting.job.glinterfaceInvestor.step.GlInterfaceInvestorReader;
import com.jrx.anytxn.accounting.job.glinterfaceInvestor.step.GlInterfaceInvestorWriter;
import com.jrx.anytxn.investor.entity.TlGlInterfaceInvestorInfo;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.Date;

/**
 *  GlInterfaceInvestorConfiguration
 *  总账流水处理 联合贷
 **/
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class GlInterfaceInvestorConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    private IOrganizationTableService organizationTableService;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Autowired
    private GlInterfaceInvestorListener glInterfaceInvestorListener;

    @Autowired
    private GlInterfaceInvestorProcessor glInterfaceInvestorProcessor;

    @Autowired
    private GlInterfaceInvestorWriter glInterfaceInvestorWriter;


    @Bean
    public Job glInterfaceInvestorJob() {
        return jobBuilderFactory.get("glInterfaceInvestorJob").incrementer(new RunIdIncrementer()).listener(glInterfaceInvestorListener)
                .start(glInterfaceInvestorStep())
                .build();
    }

    @Bean
    public Step glInterfaceInvestorStep() {
        return stepBuilderFactory.get("glInterfaceInvestorStep")
                .<TlGlInterfaceInvestorInfo, GlInterfaceInvestorBean>chunk(anytxnBatchProperties.getGlInterfaceInvestorConfig().getChunkLimit())
                .reader(glInterfaceInvestorReader())
                .processor(glInterfaceInvestorProcessor)
                .writer(glInterfaceInvestorWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getGlInterfaceInvestorConfig().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<TlGlInterfaceInvestorInfo> glInterfaceInvestorReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        Date businessDate = org.getToday();
        return new GlInterfaceInvestorReader(businessDate,sqlSessionFactory, anytxnBatchProperties.getGlInterfaceInvestorConfig().getPageSize());
    }
}
